package com.qyhl.webtv.module_user.login.normal;


import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.Md5Builder;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.dialog.LoadingDialog;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.user.UserInfoBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_user.R;
import com.qyhl.webtv.module_user.R2;
import com.qyhl.webtv.module_user.login.NewLoginActivity;
import com.qyhl.webtv.module_user.util.LoginUtils;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

/**
 * 账号密码登陆
 */
public class NormalLoginFragment extends BaseFragment implements NormalLoginContract.NormalLoginView {
    private View view;
    @BindView(R2.id.phononum)
    EditText phononum;
    @BindView(R2.id.edipassword)
    EditText edipassword;
    @BindView(R2.id.login)
    Button login;

    private LoadingDialog.Builder builder;
    private NormalLoginContract.NormalLoginPresenterModel mPresenter;


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.login_fragment_normal_login, container, false);
    }

    @Override
    public void initImmersionBar() {
    }

    @Override
    protected void create() {
        init();
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }


    private void init() {
        mPresenter = new NormalLoginPresenterModel(this);

        builder = new LoadingDialog.Builder(getActivity());
        builder.setTitle("登录中...");
        builder.setCanceledOnTouchOutside(false);
        builder.setCancelable(true);

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(edipassword.getText().toString()) ||
                        TextUtils.isEmpty(phononum.getText().toString())) {
                    login.setEnabled(false);
                } else {
                    login.setEnabled(true);
                }
            }
        };
        edipassword.addTextChangedListener(textWatcher);
        phononum.addTextChangedListener(textWatcher);
    }

    @OnClick({R2.id.register, R2.id.modifypwd, R2.id.login})
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.register) {
            //注册
            RouterManager.router(ARouterPathConstant.REGISTER, this, 888);
        } else if (i == R.id.modifypwd) {
            //找回密码
            RouterManager.router(ARouterPathConstant.NEWMODIFY);
        } else if (i == R.id.login) {
            if (getActivity() instanceof NewLoginActivity && ((NewLoginActivity) getActivity()).isAgreeRule()) {
                goLogin();
            } else {
                showToast("请认真阅读并同意《使用条款》和《隐私政策》");
            }

        } else {
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 888 && resultCode == 888) {
            getActivity().finish();
        }
    }

    private void goLogin() {
        String phone = phononum.getText().toString();
        if (!StringUtils.isMobileNO(phone)) {
            Toasty.normal(getContext(), "请输入正确的手机号！", Toast.LENGTH_SHORT).show();
            return;
        }
        String pw = edipassword.getText().toString();
        if (pw.length() < 6) {
            Toasty.normal(getContext(), "密码应为6-20位", Toast.LENGTH_SHORT).show();
            return;
        }
        builder.show();
        //加密
        String pwd = Md5Builder.encode(pw);
        mPresenter.goLogin(phone, pwd);
    }

    @Override
    public void loginError(String message) {
        builder.dismiss();
        Toasty.normal(getContext(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loginSuccess(UserInfoBean bean) {
        builder.dismiss();
        Toasty.normal(getContext(), "登录成功！", Toast.LENGTH_SHORT).show();
        LoginUtils.setloginSuccess(bean, ActionConstant.ACCOUNTLOGIN);
        getActivity().finish();
        //

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (view != null && view.getParent() != null) {
            ((ViewGroup) view.getParent()).removeView(view);
        }
        view = null;
    }
}
